package com.ruiyian.project.equipment.controller;

import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.domain.entity.Point;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.common.utils.poi.ExcelUtil;
import com.ruiyian.project.equipment.service.PointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/7/15
 * Desc: 点位控制器
 */
@Slf4j
@RestController
@RequestMapping("/equipment/point")
public class PointController extends BaseController {
    @Autowired
    private PointService pointService;

    /**
     * 分页列表
     * @param point
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:point:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Point point) {
        startPage();
        List<Point> list = pointService.selectList(point);
        return getDataTable(list);
    }

    /**
     * 保存
     * @param point
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:point:edit')")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Point point) {
        try {
            if(point.getId()!=null){
                pointService.edit(point);
            }else {
                pointService.save(point);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(point);
    }

    /**
     * 更具地获取对象
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:point:edit')")
    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        Point point = null;
        try {
            point =  pointService.findOne(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(point);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:point:remove')")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            pointService.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('equipment:point:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        List<Point> equipmentList = null;
        try {
            ExcelUtil<Point> util = new ExcelUtil<>(Point.class);
            equipmentList = util.importExcel(file.getInputStream());
            pointService.importData(equipmentList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error("导入失败");
        }
        return AjaxResult.success(equipmentList);
    }

}
